package com.example.my3;

import android.content.Context;
import android.os.Bundle;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.my3.databinding.ActivityMainBinding;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);


    }


//    public boolean setMobileDataEnabled(boolean enabled) {
//        final TelephonyManager mTelManager;
//        mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//        try {
//            Method m = mTelManager.getClass().getDeclaredMethod("getITelephony");
//            m.setAccessible(true);
//            Object telephony = m.invoke(mTelManager);
//            m = telephony.getClass().getMethod((enabled ? "enable" : "disable") + "DataConnectivity");
//            m.invoke(telephony);
//            return true;
//        } catch (Exception e) {
//            Log.e("", "cannot fake telephony", e);
//            return false;
//        }
//    }

//    @RequiresApi(23)
    public void setDefaultDataSubId(int subId) {
        SubscriptionManager subscriptionManager = (SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        try {
            Method method = subscriptionManager.getClass().getDeclaredMethod("setDefaultDataSubId", int.class);
            method.invoke(subscriptionManager, subId);
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            Method method1 = telephonyManager.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
            method1.invoke(telephonyManager, true);
        } catch (Exception e) {
            Log.e(TAG, "wjz debug setDefaultDataSubId: error is " + e.getMessage());
        }
    }

    public int getDefaultDataSubId() {
        SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        try {
            Method method = subscriptionManager.getClass().getDeclaredMethod("getDefaultDataSubscriptionId");
            return (int) method.invoke(subscriptionManager);
        } catch (Exception e) {
            Log.e(TAG, "wjz debug getDefaultDataSubId: error is " + e.getMessage());
        }
        return 0;
    }

}